
from flask_login import current_user
from flask_restful import Resource, marshal_with, reqparse
from controllers.errors.error import HTTPNotLoginError
from controllers.console import api
from core.roles.role_check import check_perms
from libs.login import login_required
from models.account import Account
from fields.robot.rob_func_fields import rob_func_fields,rob_func_pagenation_fields
from services.robot.rob_func_service import RobFuncService
class RobFuncsApi(Resource):

    @login_required
    # @check_role(base_role=AccountRole.TENANT_ADMIN)
    @check_perms(perms=['funcs:new'])
    @marshal_with(rob_func_fields)
    def post(self):
        if not isinstance(current_user,Account):
            raise HTTPNotLoginError()
        argparser = reqparse.RequestParser()
        argparser.add_argument('funcs', type=dict,action='append',required=True, location='json')
        args = argparser.parse_args()
        funcs = args.get('funcs',[])
        return RobFuncService.create_funcs(current_user,funcs)
    
    @login_required
    # @check_role(base_role=AccountRole.TENANT_ADMIN)
    @check_perms(perms=['funcs:list'])
    @marshal_with(rob_func_pagenation_fields)
    def get(self):
        if not isinstance(current_user,Account):
            raise HTTPNotLoginError()
        if not current_user.current_tenant_id:
            raise HTTPNotLoginError()
        argparser  = reqparse.RequestParser()
        argparser.add_argument("name",type=str,required=False,default=None,location="args")
        argparser.add_argument("fun_type",type=str,required=False,default=None,location="args")
        argparser.add_argument("interaction_mode",type=str,required=False,default=None,location="args")
        argparser.add_argument("need_reconize",type=bool,required=False,default=None,location="args")
        argparser.add_argument("page",type=int,default=-1,location="args")
        argparser.add_argument("page_size",type=int,default=-1,location="args")
        args = argparser.parse_args()
        name = args.get("name",None)
        fun_type = args.get("fun_type",None)
        interaction_mode = args.get("interaction_mode",None)
        need_reconize = args.get("need_reconize",None)


        page = args.get("page",-1)
        page_size = args.get("page_size",-1)
        name = args.get("name",None)
        return RobFuncService.get_funcs(current_user.current_tenant_id,name,fun_type,interaction_mode,need_reconize,page,page_size)

api.add_resource(RobFuncsApi,'/rob/funcs')